package in.muton.edushare.panels.folder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Check;
import org.apache.wicket.markup.html.form.CheckGroup;
import org.apache.wicket.markup.html.form.CheckGroupSelector;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.FileDirectoriesDAO;
import com.edushare.common.dao.FileFolderObjectDAO;
import com.edushare.common.dao.FilesUploadedDAO;
import com.edushare.common.dao.UsersDAO;
import com.edushare.common.util.DateTimeUtil;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.FileDirectories;
import com.edushare.common.vo.FileFolderObject;
import com.edushare.common.vo.FilesUploaded;
import com.edushare.common.vo.Users;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.folder.FolderHelper;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.panels.paginator.EdusharePageNavigator;

public class FileListPanel extends EdusharePanel {

	private static final long serialVersionUID = 257390180682480519L;
	private IModel<FileDirectories> dirModel;
	private EduShareBasePage parentPage;
	private Boolean isEditable;

	public FileListPanel(String id, IModel<FileDirectories> dirModel, EduShareBasePage page) {
		super(id, dirModel, page);
		this.dirModel = dirModel;
		this.parentPage = page;
		this.isEditable = FolderHelper.isEditable(dirModel.getObject(), getLoggedInUser());
		
		add(CSSPackageResource.getHeaderContribution(FileListPanel.class, "folder.css"));
		loadPanel();
	}

	private void loadPanel() {
		
		final CheckGroup<FileFolderObject> checkgroup = new CheckGroup<FileFolderObject>("checkgroup", new ArrayList<FileFolderObject>());
		Form<Void> form = new Form<Void>("ffform");
		add(form);
		form.add(checkgroup);
		CheckGroupSelector selectAll = new CheckGroupSelector("selectAll");
		checkgroup.add(selectAll);
		
		Button delete = new Button("delete"){
			
			@Override
			public void onSubmit() {
				Collection<FileFolderObject> checkedObject = checkgroup.getModelObject();
				new FileFolderObjectDAO(getORMInterface()).deleteFileOrFolder(checkedObject);
				getORMInterface().commitAllChanges();
			}
			
		};
		form.add(delete);
		
		if(!this.isEditable){
//			delete.setVisible(false);
			delete.add(new AttributeAppender("disabled", true, new Model<String>("disabled"), " "));
			delete.add(new AttributeModifier("class", true, new Model<String>("es-button ui-state-disable ui-corner-all")));
			selectAll.add(new AttributeAppender("disabled", true, new Model<String>("disabled"), " "));
		}
		
		final WebMarkupContainer noItemDiv = new WebMarkupContainer("noItemDiv");
		final PageableListView<FileFolderObject> plv = 
			new PageableListView<FileFolderObject>
				(		"fileItem", 
						new PropertyModel<List<FileFolderObject>>(FileListPanel.this, "dirFiles"),
						ApplicationConstants.PAGE_SIZE)
			{
				private static final long serialVersionUID = -3165581951906206779L;

				@Override
				protected void populateItem(final ListItem<FileFolderObject> item) {
					//col-1
					Check<FileFolderObject> check = new Check<FileFolderObject>("check", item.getModel());
					item.add(check);
					if(!isEditable){
						check.add(new AttributeAppender("disabled", true, new Model<String>("disabled"), " "));
					}
					
					//col-2
					if(item.getModelObject().getType()==FileFolderObject.FILE_TYPE){
						item.add(new FileTitleFrag("title", FileListPanel.this, item.getModel()));
					}else{
						item.add(new FolderTitleFrag("title", FileListPanel.this, item.getModel()));
					}
					
					//col-3
					item.add(new Label("fileVis", FolderHelper.getFolderReadPermissionString(item.getModelObject().getVisibility())));
					if(item.getModelObject().getCreatorId()==null || item.getModelObject().getCreatorId()==0){
						item.add(new Label("fileCreator", "Not Available"));
					}else{
						item.add(new EduShareURLPanel("fileCreator", new Model<Users>(){
										public Users getObject()
											{
												return new UsersDAO(getORMInterface())
												.getUserByPk
													(item
													.getModelObject()
													.getCreatorId());
											}
										}));
					}
					
					item.add(new Label("fileDOB", DateTimeUtil.formatTimestamp(item.getModelObject().getDateCreation())));
					//col-4
					item.add(new Label("fileLastUpdated", item.getModelObject().getDateUpdatedStr()));
				}
				
				@Override
				protected void onBeforeRender() {
					if(this.getList()==null || this.getList().size()<1){
						noItemDiv.setVisible(true);
					}else{
						noItemDiv.setVisible(false);
					}
					super.onBeforeRender();
				}
					
			};
			
		checkgroup.add(plv);
		
		
		checkgroup.add(noItemDiv);
		checkgroup.add(new EdusharePageNavigator("filepagination", plv));
	}
	
	public List<FileFolderObject> getDirFiles(){
		List<FilesUploaded> fuList = new FilesUploadedDAO(getORMInterface()).getFiles(dirModel.getObject());
		List<String> userVisibility = FolderHelper.getUserVisibilityUnderDir(getLoggedInUser(), this.dirModel.getObject());
		List<FileDirectories> fdList = new FileDirectoriesDAO(getORMInterface()).getChildren(this.dirModel.getObject(), userVisibility);
		List<FileFolderObject> fifoList = FolderHelper.mergeLists(fuList, fdList);
		return fifoList;
	}
	
	public class FolderTitleFrag extends Fragment{

		private static final long serialVersionUID = 7666000591818858555L;

		public FolderTitleFrag(String id, MarkupContainer markupProvider, IModel<FileFolderObject> model) {
			super(id, "folderTitle", markupProvider, model);
			FileDirectories fd = new FileDirectoriesDAO(getORMInterface()).getFileDirById(model.getObject().getId());
			BookmarkablePageLink<Void> bpl = new BookmarkablePageLink<Void>
												(
												"folderLink", 
												parentPage.getClass(), 
												FolderHelper
													.getDirectoryPageParameters(fd));
			add(bpl);
			bpl.add(new Label("titleText", ((FileFolderObject)getDefaultModelObject()).getTitle()));
		}
		
	}
	
	public class FileTitleFrag extends Fragment{

		private static final long serialVersionUID = 5871367916384538426L;

		public FileTitleFrag(String id, MarkupContainer markupProvider, IModel<FileFolderObject> model) {
			super(id, "fileTitle", markupProvider, model);
			ExternalLink dldLink = new ExternalLink(
											"fileLink", 
											((FileFolderObject)getDefaultModelObject()).getUrl(),
											((FileFolderObject)getDefaultModelObject()).getTitle());
			add(dldLink);
		}
		
	}

}
